/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.panomedic.gui;

import com.panomedic.utils.Utils;
import com.panomedic.core.Photo;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

/**
 *
 * @author Yare
 */
public class ThumbPanel extends JPanel implements ListCellRenderer {

    protected Photo photo;
    protected boolean hasFocus = false;
    protected boolean selected = false;
    protected BufferedImage thumb = null;

    public void paint(Graphics g) {

        int cellWidth = getWidth();
        int cellHeight = getHeight();

        Graphics2D g2 = (Graphics2D) g;
        g2.setBackground(Color.BLACK);              //const

        if (selected) {
            g2.setColor(getBackground());
            g2.fillRect(0, 0, cellWidth, cellHeight);

        }

        int border = 3;
        int nameSpace = 20;
        if (photo == null) {
            return;
        }
        Dimension limits = new Dimension(cellWidth - border * 2, cellHeight - border * 2 - nameSpace);

        thumb = photo.getThumb();
//        logger.info(LogCategories.GUI, "Loaded thumb: "+photo);
        if (thumb != null) {
            int w = thumb.getWidth();
            int h = thumb.getHeight();

            Dimension dim = Utils.getScaledSize(limits, w, h);
            int x = (cellWidth - dim.width) / 2;
            int y = (cellHeight - dim.height) / 2 - nameSpace / 2;

            g2.setColor(Color.DARK_GRAY);            //const
            g2.fillRect(x - border, y - border, dim.width + 2 * border, dim.height + 2 * border);
            g2.drawImage(thumb, x, y, dim.width, dim.height, this);
            
            g2.setColor(Color.BLACK);
            g2.drawString(photo.toString(), x, y + dim.height + 2 * border + 10);


        }


    }

    public Component getListCellRendererComponent(JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        ThumbList photolist = (ThumbList) list;
        setSelected(isSelected);
        setHasFocus(cellHasFocus);
        setPhoto((Photo) value);


        return this;
    }

    private void setHasFocus(boolean cellHasFocus) {
        this.hasFocus = cellHasFocus;
    }

    private void setPhoto(Photo photo) {
        this.photo = photo;
        repaint(0, 0, 0, getWidth(), getHeight());
    }

    private void setSelected(boolean selected) {
        this.selected = selected;
    }
}
